/**
 * Recurses directory and returns .js files
 *  - ensures that subdirs(and files) are appended to array
 *  - you have to ensure your root directory contains an app.js that gets loaded at the start
 */

var sys = require ('sys'), fs = require ('fs');
function readDir(dir) {
	dir = dir.replace(/\/$/,'');
	var files = [], dirs = [], tmp = [];
	tmp = fs.readdirSync(dir).sort();
	tmp.forEach(function(f) {
		var path = dir + "/" + f;
		if (/js$/.test(path) && !(/02-gmapapiv3.js/.test(path))) { 
			files.push(path); 
		}
		
		if (isDir(path)) { dirs.push(path); } 
	});
	dirs.forEach(function(d) { files = files.concat(readDir(d)); });
	return files;
}

function isDir(path) {
	if ((fs.statSync(path).mode & 0x4000) !== 0) { return true; }
	return false;
}
exports.isDir = isDir;
exports.readDir = readDir;
